package com.sunday.pinxinghui.widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.adapter.CommenAdapter;
import com.sunday.common.adapter.ViewHolder;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.utils.PixUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.MyWindow;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.base.BaseApplication;
import com.sunday.pinxinghui.entity.Member;
import com.sunday.pinxinghui.entity.ProductDetail;
import com.sunday.pinxinghui.entity.ProductNormal;
import com.sunday.pinxinghui.entity.Spec;
import com.sunday.pinxinghui.network.ApiClient;

import java.math.RoundingMode;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Author by Damon,  on 2017/11/22.
 * Email 372128283@qq.com
 */

public class UpdateCartNumWindow extends PopupWindow {


    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.minus)
    TextView minus;
    @Bind(R.id.num)
    EditText numT;
    @Bind(R.id.add)
    TextView add;
    @Bind(R.id.ll_update)
    LinearLayout llUpdate;
    @Bind(R.id.btn_cancel)
    TextView btnCancel;
    @Bind(R.id.btn_confirm)
    TextView btnConfirm;

    private Context mContext;
    private int maxCount;

    private Click click;

    private Member member;
    private View contentView;

    private int num = 1;
    private int orginNum;
    private int position;

    public UpdateCartNumWindow(Context context,Click click,int nums,int p) {
        mContext = context;
        this.click = click;
        this.num=nums;
        this.orginNum=nums;
        this.position=p;
        LayoutInflater mLayoutInflater = LayoutInflater.from(context);
        contentView = mLayoutInflater.inflate(R.layout.update_cart_window, null);
        int appWidth = DeviceUtils.getDisplay(mContext).widthPixels;
        setContentView(contentView);
        ButterKnife.bind(this, contentView);
        this.setWidth(appWidth-PixUtils.dip2px(mContext,70));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setOnDismissListener(new PoponDismissListener());
        this.setBackgroundDrawable(new BitmapDrawable());
        member = BaseApplication.getInstance().getMember();
        numT.setText(String.valueOf(orginNum));
        numT.requestFocus();
        numT.setSelection(numT.getText().length());
        numT.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String number = String.valueOf(s);
                if (TextUtils.isEmpty(number)) {
                    num = 0;
                } else if (number.startsWith("0")) {
                    numT.setText(number.substring(1, number.length()));
                    num = Integer.valueOf(numT.getText().toString().trim());
                } else {
                    num = Integer.valueOf(numT.getText().toString().trim());
                }
                numT.setSelection(numT.getText().length());
            }
        });

    }

    //显示
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            this.showAtLocation(parent, Gravity.CENTER, 0, 0);
            backgroundAlpha(0.5f);
        } else {
            this.dismiss();
        }
    }

    @OnClick({R.id.btn_cancel, R.id.btn_confirm, R.id.minus, R.id.add})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                this.dismiss();
                break;
            case R.id.btn_confirm:
                String num1=numT.getText().toString().trim();
                if (click!=null){
                    click.click(orginNum,TextUtils.isEmpty(num1)?0:num-orginNum,position);
                }
                this.dismiss();
                break;
            case R.id.minus:
                if (num <= 1) {
                    return;
                }
                num--;
                numT.setText(num + "");
                break;
            case R.id.add:
                num++;
                numT.setText(num + "");
                break;
        }
    }





    public interface Click {
        public void click(int originNum,int num,int p);
    }

    /**
     * 弹出的popWin关闭的事件，主要是为了将背景透明度改回来
     */
    class PoponDismissListener implements PopupWindow.OnDismissListener {

        @Override
        public void onDismiss() {
            // TODO Auto-generated method stub
            //Log.v("List_noteTypeActivity:", "我是关闭事件");
            backgroundAlpha(1f);
            dismiss();
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

}
